const express = require('express');
const router = express.Router();
const sql = require("../sql/index");
const utils = require("../utils/index");
const Order = require("../sql/collection/Order");
const randomstring = require('randomstring');

/**
* @api {post} /api/order/add 生成订单数据
* @apiName PostApiOrder
* @apiGroup Order
* 
* @apiParam { String } userid 用户ID
* @apiParam { Array } list 订单商品信息
* 
* @apiSuccessExample { json } Success-Response:
        res.send({
            code: '200',
            message: '生成订单数据',
            time
        })
* @apiSampleRequest /api/order/add
* @apiVersion 0.0.0
*/
// 生成订单数据
router.post('/add', (req, res, next) => {
    let { userid, list } = req.body;
    const time = Date.now();
    utils.verifyToken(req, res).then(() => {
        list.forEach(val => {
            val.userid = userid;
            val.orderid = 'order_' + randomstring.generate(13) + time;
            val.status = 0; // 0待支付 1待发货 2已完成
            val.name = ''; // 收货人
            val.tel = ''; // 手机号
            val.address = ''; // 地址
            val.time = time; // 生成订单的唯一时间 时间戳
        })
        sql.insert(Order, list).then(() => {
            res.send({
                code: '200',
                message: '生成订单数据',
                time
            })
        })
    })
})
/**
* @api {post} /api/order/orderlistdata 获取订单列表数据
* @apiName PostApiOrderListData
* @apiGroup Order
* 
* @apiParam { String } time 生产订单的时间
* 
* @apiSuccessExample { json } Success-Response:
        res.send({
            code: '200',
            message: '获取订单列表数据'
        })
* @apiSampleRequest /api/order/orderlistdata
* @apiVersion 0.0.0
*/
// 获取订单列表数据
router.post('/orderlistdata', (req, res, next) => {
    let { time } = req.body;
    utils.verifyToken(req, res).then(() => {
        sql.find(Order, { time }, { _id: 0 } ).then(data => {
            res.send({
                code: '200',
                message: '订单列表数据',
                data
            })
        })
    })
})

/**
* @api {post} /api/order/updateorderdata 修改订单信息
* @apiName PostApiUpdateOrderData
* @apiGroup Address
* 
* @apiParam { String } userid 用户ID
* @apiParam { String } name 收件人
* @apiParam { String } tel 手机号
* @apiParam { String } address 地址
* @apiParam { String } time 生成订单的时间唯一标识
* 
* @apiSuccessExample { json } Success-Response:
        res.send({
            code: '200',
            message: '修改订单信息成功'
        })
* @apiSampleRequest /api/order/updateorderdata
* @apiVersion 0.0.0
*/
// 修改订单信息
router.post('/updateorderdata', (req, res, next) => {
    utils.verifyToken(req, res).then(() => {
        const { userid, name, tel, address, time } = req.body
        sql.update(Order, { userid, time }, { $set: { name, tel, address } }, 1).then(() => {
            res.send({
                code: '200',
                message: '修改订单信息成功'
            })
        })
    })
})
/**
* @api {post} /api/order/deleteorderdata 删除订单数据
* @apiName PostApiDeleteOrderData
* @apiGroup Address
* 
* @apiParam { String } userid 用户ID
* @apiParam { String } time 生成订单的时间唯一标识
* 
* @apiSuccessExample { json } Success-Response:
        res.send({
            code: '200',
            message: '删除订单数据'
        })
* @apiSampleRequest /api/order/deleteorderdata
* @apiVersion 0.0.0
*/
// 删除订单数据
router.post('/deleteorderdata', (req, res, next) => {
    utils.verifyToken(req, res).then(() => {
        const { userid, time } = req.body
        sql.delete(Order, { userid, time }, 1).then(() => {
            res.send({
                code: '200',
                message: '删除订单数据'
            })
        })
    })
})
module.exports = router;
